﻿using System;
using System.Collections.Generic;

namespace ProjetCom.Model.Snake
{

    class Snake {
        public enum Direction { Up, Down, Right, Left, }
        private List<BlockSnake> m_snakeList;
        private Direction m_curentDirection;
        private Direction m_lastDirection;


        public List<BlockSnake> ListeBlock
        {
            get { return m_snakeList; }
        }

        public Direction CurentDirection
        {
            get { return m_curentDirection; }
            set { m_curentDirection = value; }
        }
        public Direction LastDirection
        {
            get { return m_lastDirection; }
        }

        public BlockSnake Head
        {
            get { return m_snakeList[0]; }
        }



        public Snake()
        {
            m_curentDirection = Direction.Right;
            m_lastDirection = Direction.Left;
            m_snakeList = new List<BlockSnake>();
            m_snakeList.Add(new BlockSnake(BlockName.Head, 9, 5));
            m_snakeList.Add(new BlockSnake(BlockName.Tail, 8, 5));
            m_snakeList.Add(new BlockSnake(BlockName.Tail, 7, 5));
        }

        /// <summary>
        /// Avance le snake
        /// </summary>
        /// <param name="p_isEating">Si on a mangé un obstacle</param>
         public void AvancerSnake(Boolean p_isEating)
        {
            int newX = m_snakeList[0].X;
            int newY = m_snakeList[0].Y;

            // On change l'affichage de la tête
            m_snakeList[0].ChangePosition();

            // On change l'affichage de l'avant dernier élément de la queux on supprime le dernier élément de la queux précédente, s'il y a pas de nouveau élément
            if (!p_isEating)
            {
                m_snakeList[m_snakeList.Count - 2].ChangePosition();
                m_snakeList.RemoveAt(m_snakeList.Count - 1);
            }

            switch (m_curentDirection)
            {
                case Direction.Down:
                    newY += 1;
                    break;
                case Direction.Up:
                    newY -= 1;
                    break;
                case Direction.Left:
                    newX -= 1;
                    break;
                case Direction.Right:
                    newX += 1;
                    break;
            }

            // Calcul des nouvelles coordonnées
            if (newX < 0)
                newX = 19;
            else if (newX > 19)
                newX = 0;
            if (newY < 0)
                newY = 10;
            else if (newY > 10)
                newY = 0;

            // On ajoute la tête
            m_snakeList.Insert(0, new BlockSnake(BlockName.Head, newX, newY));

            // On sauvegarde la dernière direction
            m_lastDirection = m_curentDirection;
        }



    }
}
